<?php


namespace Home\Controller;
use Think\Controller;

/**
 * 前台公共控制器
 * 为防止多分组Controller名称冲突，公共Controller名称统一使用分组名称
 */
class HomeController extends Controller {

	/* 空操作，用于输出404页面 */
	public function _empty(){
		$this->redirect('Index/index');
	}


    protected function _initialize(){
        /* 读取站点配置 */
        $config = api('Config/lists');
        C($config); //添加配置

        if(!C('WEB_SITE_CLOSE')){
            $this->error('站点已经关闭，请稍后访问~');
        }
        $this->sjsp();
        $this->goshu();
    }

	/* 用户登录检测 */
	protected function login(){
		/* 用户登录检测 */
		is_login() || $this->error('您还没有登录，请先登录！', U('User/login'));
	}

	/**
 * 获取参数的所有父级分类
 * @param int $cid 分类id
 * @return array 参数分类和父类的信息集合
 * @author huajie <banhuajie@163.com>
 */

    function get_parent_category($cid){
    if(empty($cid)){
        return false;
    }
    $cates  =   M('cateshop')->where(array('status'=>1))->field('id,title,pid')->order('sort')->select();
    // var_dump($cates);
    $child  =   get_category($cid); //获取参数分类的信息
    // var_dump($child);
    $pid    =   $child['pid'];
    $temp   =   array();
    $res[]  =   $child;
    while(true){
        foreach ($cates as $key=>$cate){
            if($cate['id'] == $pid){
                $pid = $cate['pid'];
                array_unshift($res, $cate); //将父分类插入到数组第一个元素前
            }
        }
        if($pid == 0){
            break;
        }
    }

    // var_dump($res);die;
    return $res;

}

public function goshu($table="carshop"){
      $map=array('status'=>1,'username'=>get_username());
      $goshu = M($table)->where($map)->count("id");
      // var_dump($goshu,M()->getLastSql());
      $this->assign('goshu',$goshu);//购物车数
      $this->fetch('/Public/header');
}

public function sjsp($table="goodshop"){

      $sjsp = M($table)->order('rand()')->limit('5')->select();
      $sjsp = $this->bltu($sjsp);
      $sjsp = $this->lowprice($sjsp);
      
      $this->assign('sjsp',$sjsp);//随机商品
      $this->fetch('/Public/header');
}


public function xsqg($table="goodshop",$status = 1,$endtime=0){
      $map['status'] = array('eq',"$status");
      $map['endtime'] =array('neq',"$endtime");
      $xsqg = M($table)->where($map)->limit('5')->select();
      $xsqg = $this->bltu($xsqg);
      $xsqg = $this->lowprice($xsqg);
      $xsqg = $this->djs($xsqg);
      // var_dump($xsqg);
      $this->assign('xsqg',$xsqg);//限时抢购
}

public function ishot($table="goodshop",$status = 1,$ishot= 1){
     $map = array('status' => $status, 'ishot' => $ishot);
      $ishot = M($table)->where($map)->limit('5')->select();
      $ishot = $this->bltu($ishot);
      $ishot = $this->lowprice($ishot);
      // var_dump($ishot);
      $this->assign('ishot',$ishot);//热卖商品
}


public function djs($time){
  foreach ($time as $k => $v) {
    $time[$k]['time'] = $time[$k]['endtime']- $time[$k]['ctime'];
  }
  
  return $time ;
}





//遍历图片
      public function bltu($list){
        if(!(is_array($list['0']))){
                     $a = $list['pic'];
                     if(strrpos($a, ",")){
                  $list['pic'] =explode(',', $a) ;
                }
        }else{
          foreach($list as $k=>$v){
                $a = $list[$k]['pic'];
                if(strrpos($a, ",")){
                  $list[$k]['pic'] =explode(',', $a) ;
                };
                // $list[$k]['gname'] = $show[$k]['title'];
              }
        }
              return $list;
      }

      //最低价格
      public function lowprice($good){

        if (!(is_array($good['0']))) {
          $gid = $good['id'];
                  $sprice = M("attrprice")->field('sprice')->where("gid = $gid")->min('sprice');
                  $mprice = M("attrprice")->field('mprice')->where("gid = $gid")->min('mprice');
                  $good['sprice'] = $sprice;
                  $good['mprice'] = $mprice;
        }else{
          foreach ($good as $k => $v) {
                  $gid = $good[$k]['id'];
                  $sprice = M("attrprice")->field('sprice')->where("gid = $gid")->min('sprice');
                  $mprice = M("attrprice")->field('mprice')->where("gid = $gid")->min('mprice');
                  $good[$k]['sprice'] = $sprice;
                  $good[$k]['mprice'] = $mprice;
        }
                 }
                 return $good;
      }




}
